﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IPfiri
{
    
    //The client pool seen as a list of unique clients
    class ClientPool
    {
        
        private static readonly List<Client> Pool = new List<Client>(30);
        private static ClientPool instance;
        public static ClientPool Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new ClientPool();
                }
                return instance;
            }
        }
        public Client extractFromPool(string nume, string varsta)
        {
            Client extracted = null ;


            if (Client.howMany()  >= 30 && Pool.Count > 0)
            {
                int found = 0;
                foreach (Client c in Pool)
                {
                    if (c.getNume() == nume && c.getVarsta() == varsta)
                    {
                        
                        extracted = c;
                        Client.removeClient();
                        return extracted;
                        found = 1;
                    }
                }
                if ( found == 0 )extracted = newClient(nume, varsta);
            }
            else
            {
                extracted = newClient(nume, varsta);
            }
            return extracted;
        }

        private Client newClient(string nume, string varsta)
        {

            Client newObject = new Client();
            newObject.setNume(nume);
            newObject.setVarsta(varsta);
            Pool.Add(newObject);
            return newObject;
        }
    }
}
